package com.scut.campus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.scut.campus.context.BaseContext;
import com.scut.campus.entity.Like;
import com.scut.campus.mapper.CommentMapper;
import com.scut.campus.mapper.LikeMapper;
import com.scut.campus.mapper.PostMapper;
import com.scut.campus.service.LikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class LikeServiceImpl extends ServiceImpl<LikeMapper, Like> implements LikeService {
    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private PostMapper postMapper;

    @Override
    public void insertPostLike(String postId) {
        Like like = Like.builder()
                .userId(BaseContext.getCurrentId())
                .postId(postId)
                .lastId(postMapper.selectById(postId).getUserId())
                .build();
        this.save(like);
    }

    @Override
    public void likeComment(String commentId) {
        Like like = Like.builder()
                .commentId(commentId)
                .userId(BaseContext.getCurrentId())
                .lastId(commentMapper.selectById(commentId).getUserId())
                .build();
        this.save(like);
    }

    @Override
    public void deleteByPostId(String postId, boolean verifyId) {
        LambdaQueryWrapper<Like> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Like::getPostId, postId);
        if (verifyId) {
            lambdaQueryWrapper.eq(Like::getUserId, BaseContext.getCurrentId());
        }
        this.remove(lambdaQueryWrapper);
    }

    @Override
    public void deleteByCommentId(String commentId, boolean verifyId) {
        LambdaQueryWrapper<Like> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Like::getCommentId, commentId);
        if (verifyId) {
            lambdaQueryWrapper.eq(Like::getUserId, BaseContext.getCurrentId());
        }
        this.remove(lambdaQueryWrapper);
    }

    @Override
    public int countPostLike(String postId) {
        LambdaQueryWrapper<Like> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper
                .eq(Like::getPostId, postId);
        return (int) this.count(lambdaQueryWrapper);
    }

    @Override
    public int countCommentLike(String commentId) {
        LambdaQueryWrapper<Like> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper
                .eq(Like::getCommentId, commentId);
        return (int) this.count(lambdaQueryWrapper);
    }

    @Override
    public Map<String, String> isPostLiked(List<String> postIdList) {
        LambdaQueryWrapper<Like> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        lambdaQueryWrapper
                .select(Like::getUserId, Like::getPostId)
                .in(Like::getPostId, postIdList)
                .eq(Like::getUserId, BaseContext.getCurrentId());
        List<Like> likeList = this.list(lambdaQueryWrapper);

        return likeList.stream().collect(
                Collectors.toMap(Like::getUserId, Like::getPostId)
        );
    }

    @Override
    public Map<String, String> isCommenttLiked(List<String> commentIdList) {
        LambdaQueryWrapper<Like> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        lambdaQueryWrapper
                .select(Like::getUserId, Like::getCommentId)
                .in(Like::getCommentId, commentIdList)
                .eq(Like::getUserId, BaseContext.getCurrentId());
        List<Like> likeList = this.list(lambdaQueryWrapper);

        return likeList.stream().collect(
                Collectors.toMap(Like::getUserId, Like::getCommentId)
        );
    }
}
